package no.uio.ifi.cop.common;

import java.io.PrintStream;
import java.util.HashMap;

/**
 * Global variables.
 *
 * This class wraps System Properties, with some properties
 * which are useful for this program, i.e., you have System
 * Properties and some other properties in one place.
 *
 * @version 1.0
 * @author  bjarneh@ifi.uio.no
 */

/*  properties that can be found on any running JVM
    as long as it's not some JNLP Webstart stuff

    java.class.path
    java.home
    user.dir
    user.home
    user.name
    java.class.version
    java.vendor
    java.vendor.url
    java.version
    os.name
    os.arch
    os.version
    file.separator
    path.separator
    line.separator


    properties added for the prover itself
    more will probably be added

    coprc          -  config file              (default: $HOME/.coprc)
    storetree      -  store proof tree         (default: false)
    dynamic        -  resorting of rules       (default: false)
    version        -  store version string     (default: 0.2)
    -timeout       -  timeout value            (default: 2 seconds)
    -recursive     -  recursive path-walk      (default: false)
    -hidden        -  include hidden files     (default: false)
    -loglevel      -  loglevel during run      (default: WARNING)
    -logfile       -  additional logfile       (default: "")
    -strategy      -  simple,rete...           (default: main)
    -dotfile       -  name of dotfile          (default: "")
    -dotmax        -  print at most N nodes    (default: "")
    -cmp           -  comparator class         (default: "natural")
    -plain         -  no ANSI color            (default: false)
    -seed          -  seed for Random          (default: 0)
    -listcmp       -  list comparators         (default: false)
    -dotheader     -  fontsize dot-header      (default: 25)
    -paths         -  print path-count         (default: false)
    -order         -  hash,time,random         (default: hash)
    -single        -  match one fact at a time (default: false)
    -dotfmt        -  convert dotfile to fmt   (default: eps)
    -maxpaths      -  max open paths at once   (default: 1000)
    -ignore        -  ignore used facts        (default: false)
    -accurate      -  accurate timing results  (default: false)

*/


public class Global{

    private static HashMap<String,String> map;

    private Global(){}

    static{
        init();
    }

    private static void init(){
        map = new HashMap<String, String>();
        map.put("coprc", 
                System.getProperty("user.home") +
                System.getProperty("file.separator")+
                ".coprc");
        map.put("storetree", "false");
        map.put("dynamic", "false");
        map.put("-timeout", "2");
        map.put("-recursive", "false");
        map.put("-hidden", "false");
        map.put("-dotfile", "");
        map.put("-dotheader", "25");
        map.put("-dotmax", "");
        map.put("-loglevel", "WARNING");
        map.put("-logfile", ""); // no logfile is default
        map.put("-strategy", "main");
        map.put("-cmp", "natural");
        map.put("-dotfmt", "eps");
        map.put("-plain", "false");
        map.put("-seed", "0");
        map.put("-listcmp", "false");
        map.put("-paths", "false");
        map.put("-order", "hash");
        map.put("-single", "false");
        map.put("-maxpaths", "1000");
        map.put("-ignore", "false");
        map.put("-accurate", "false");
        map.put("version", "cop 0.2");
    }

    public static String get(String key){
        if(map.containsKey(key)){
            return map.get(key);
        }

        if(System.getProperties().containsKey(key)){
            return System.getProperty(key);
        }
        return null;
    }

    public static boolean getBool(String key){

        if( map.containsKey(key)){
            try{
                return Boolean.parseBoolean( map.get(key) );
            }catch(Exception e){
                Log.fatal("%s", e);
            }
        }
        return false;
    }

    public static double getDouble(String key){

        if( map.containsKey(key)){
            try{
                return Double.parseDouble( map.get(key) );
            }catch(Exception e){
                Log.fatal("%s", e);
            }
        }
        return -1.0;
    }
    
    public static int getInt(String key){
        if( map.containsKey(key)){
            try{
                return Integer.parseInt( map.get(key) );
            }catch(Exception e){
                Log.fatal("%s", e);
            }
        }
        return -1;
    }

    public static long getLong(String key){
        if( map.containsKey(key)){
            try{
                return Long.parseLong( map.get(key) );
            }catch(Exception e){
                Log.fatal("%s", e);
            }
        }
        return -1;
    }

    public static void set(String key, String value){
        map.put(key,value);
    }

    public static void list(PrintStream out){
        out.println("Java Properties");
        System.getProperties().list(out);
        out.println("-- config properties --");
        for(String key : map.keySet()){
            if(key.startsWith("-")){
                out.printf("%s=%s\n", key, map.get(key));
            }
        }
    }
}
